home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / os2 / htmle96b.zip / TOUPPER.CMD < prev   
OS/2 REXX Batch file  |  1996-07-22  |  6KB  |  166 lines

  1. /*                                                                 */
  2. /* TOUPPER.CMD (v1.01):  Convert filenames to upper/lowercase      */
  3. /* Ian Prest                                                       */
  4. /* June 26, 1996 (v1.0)                                            */
  5. /* June 29, 1996 (v1.01)                                           */
  6. /*                                                                 */
  7. /* Notice the nice layout/framework that will                      */
  8. /* simplify creating other utilities of this type                  */
  9. /* (utilities that perform an action on multiple                   */
  10. /* filespecs, and have multiple switches).                         */
  11. /*                                                                 */
  12.  
  13. /* Load rexxutil functions                                         */
  14. call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  15. call SysLoadFuncs
  16. '@echo off'
  17.  
  18. /* Display copyright info                                          */
  19. call proginfo
  20.  
  21. /* set up default state for "switch" variables                     */
  22. parameters = 'O'
  23. bfd = 0
  24. recurse = 0
  25. ulcase = 0
  26.  
  27. /* parse switches, which will modify state of "switch" variables   */
  28. PARSE UPPER ARG cmdline
  29. call parseargs cmdline, 0 
  30.  
  31. /* set up global variables based on "switch" variable state        */
  32.  
  33. /* handle BFD parameters                                           */
  34. if bfd=0 then parameters = parameters'B'
  35. else if bfd=1 then parameters = parameters'F'
  36. else if bfd=2 then parameters = parameters'D'
  37.  
  38. /* handle RECURSE parameter                                        */
  39. if recurse=1 then parameters = parameters'S'
  40.  
  41. /* handle UPPER/LOWERCASE parameters                               */
  42. if ulcase=0 then do
  43.    tableo = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  44.    tablei = "abcdefghijklmnopqrstuvwxyz"
  45.    end
  46. else do
  47.    tableo = "abcdefghijklmnopqrstuvwxyz"
  48.    tablei = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  49.    end
  50.  
  51. /* set number of filespecs encountered thus far to zero            */
  52. fscount = 0
  53.  
  54. /* parse filenames, which will perform the action on the files and */
  55. /* increase "fscount"                                              */
  56. PARSE UPPER ARG cmdline
  57. call parseargs cmdline, 1  
  58.  
  59. /* if fscount=0 then there weren't any filespecs on the cmd-line   */
  60. /* so we tell user what to do                                      */
  61. if fscount=0 then call usage
  62.  
  63. /* we're done!  yay!                                               */
  64. exit
  65.  
  66.  
  67. /*                                                                 */
  68. /* ***** Subroutines! *****                                        */
  69. /*                                                                 */
  70.  
  71. /* Informs user about command line parameters!                     */
  72. usage: procedure
  73.    say 'Usage:  TOUPPER  <filespec> [<filespec> [...]] [/rdfblu]'
  74.    say
  75.    say '  /r  Scans directories recursively'
  76.    say '  /n  No directory recurse (default)'
  77.    say '  /d  Directories only'
  78.    say '  /f  Files only'
  79.    say '  /b  Both files and directories (default)'
  80.    say '  /l  Convert to lowercase'
  81.    say '  /u  Convert to uppercase (default)'
  82.    return
  83.  
  84. /* print copyright & other program info to the screen              */
  85. proginfo: procedure
  86.    say 'TOUPPER v1.01 - change the case of filenames'
  87.    say 'Copyright (C) 1996  Ian Prest'
  88.    say 'All rights reserved.'
  89.    say
  90.    return
  91.  
  92. /* performs the action on a file                                   */
  93. fileaction: arg file
  94.    name = file
  95.  
  96.    /* trim off pathname                                            */
  97.    name = filespec("name",name)
  98.  
  99.    /* convert case                                                 */
  100.    name = TRANSLATE(name, tableo, tablei)
  101.  
  102.    /* if the name has changed, rename file                         */
  103.    if name <> filespec("name",file.i) then do
  104.       say file.i '-->' name
  105.       'rename "'file.i'" "'name'"'
  106.       end
  107.    return
  108.  
  109. /* performs the action on a filespec                               */
  110. filespecaction: arg filespec
  111.    /* "file" is a stem variable to hold all the names...           */
  112.    call SysFileTree filespec, 'file', parameters, "**---"
  113.  
  114.    if file.0=0 then do
  115.       say filespec":  No matching files found."
  116.       end
  117.    else do
  118.       say filespec":  "file.0" matching files found."
  119.       do i=1 to file.0
  120.          call fileaction file.i
  121.          end
  122.       end
  123.    say
  124.    return
  125.  
  126. /* interprets a single character command-line switch               */
  127. interpretswitch: arg s
  128.    if s="N" then recurse=0
  129.    else if s="R" then recurse=1
  130.    else if s="B" then bfd = 0
  131.    else if s="F" then bfd = 1
  132.    else if s="D" then bfd = 2
  133.    else if s="U" then ulcase = 0
  134.    else if s="L" then ulcase = 1
  135.    else do
  136.       say "Invalid parameter '/"s"'"
  137.       say
  138.       call usage
  139.       exit
  140.       end
  141.    return
  142.  
  143. /* parse command-line                                              */
  144. parseargs: arg switches, argtype
  145.    do forever
  146.       PARSE VAR switches sw switches
  147.       if sw="" then leave
  148.  
  149.       /* argtype=0 means we're looking for switches                */
  150.       if argtype=0 then do
  151.          if substr(sw,1,1) = "/" | substr(sw,1,1)="-" then
  152.             do swstart=2 to length(sw)
  153.                s = substr(sw,swstart,1)
  154.                call interpretswitch s
  155.                end
  156.          end
  157.       /* argtype=1 means we're looking for filespecs               */
  158.       else if argtype=1 then do
  159.          if \ (substr(sw,1,1) = "/" | substr(sw,1,1)="-") then do
  160.             fscount = fscount + 1
  161.             call filespecaction sw
  162.             end
  163.          end
  164.       end
  165.    return
  166.